<template>
  <section>
    <el-breadcrumb class="app-breadcrumb" separator="/">
      <transition-group name="breadcrumb">
        <el-breadcrumb-item v-for="(item,index) in levelList" :key="item.path">
          <span
            v-if="item.redirect==='noredirect'||index==levelList.length-1"
            class="no-redirect"
          >{{ item.meta.title === 'query' ? params.name : item.meta.title }}</span>
          <!-- <a v-else @click.prevent="handleLink(item)">{{ item.meta.title === 'params' ? $route.params.name : item.meta.title }}</a> -->
           <a v-else @click.prevent="handleLink(item)">{{ item.meta.title === 'params' ? params.name : item.meta.title }}</a>
        </el-breadcrumb-item>
      </transition-group>
    </el-breadcrumb>
    <p class="page-name">{{ $route.meta.title === 'query' ? $route.meta.pageName : $route.meta.title}}</p>
  </section>
</template>

<script>
import pathToRegexp from "path-to-regexp";
import { cryptoJSDecrypt } from '@/utils/index'

export default {
  data() {
    return {
      levelList: null
    };
  },
  watch: {
    $route() {
      this.getBreadcrumb();
    }
  },
  computed: {
    params: {
      get () {
        if (this.$route.query.params) {
          return cryptoJSDecrypt(this.$route.query.params)
        }
        return this.$route.query
      }
    }
  },
  created() {
    this.getBreadcrumb();
  },
  methods: {
    getBreadcrumb() {
      let matched = this.$route.matched.filter(item => item.name);

      this.levelList = matched.filter(
        item => item.meta && item.meta.title && item.meta.breadcrumb !== false
      );
    },
    pathCompile(path) {
      const { params } = this.$route;
      var toPath = pathToRegexp.compile(path);
      return toPath(params);
    },
    handleLink(item) {
      const { redirect, path } = item;
      if (redirect) {
        this.$router.push(redirect);
        return;
      }
      this.$router.push(this.pathCompile(path));
    }
  }
};
</script>

<style lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 12px;
  margin-left: 8px;
  color: #A4A7B8;
  padding: 16px 0;
  span, a {
    font-weight: bold !important;
  }

  .no-redirect {
    color: #757780;
    cursor: text;
  }
}
.page-name {
  padding-left: 7px;
}
</style>
